﻿namespace ImageCropper.Util
{
    public static class DataParser
    {
        public static double DoubleParse(this string number, bool returnOne)
        {
            double value;

            if (!double.TryParse(number, out value))
            {
                value = (returnOne) ? 1 : 0;
            }

            return value;
        }

        public static double DoubleParse(this string number)
        {
            return DoubleParse(number, false);
        }

        public static double? NullableDoubleParse(this string number)
        {
            if (string.IsNullOrWhiteSpace(number))
            {
                return null;
            }

            return DoubleParse(number, false);
        }

        public static int IntParse(this string number, bool returnOne)
        {
            int value;

            int dotPos = number.IndexOf('.');

            if (dotPos > 0)
            {
                number = number.Remove(dotPos);
            }

            if (!int.TryParse(number, out value))
            {
                value = (returnOne) ? 1 : 0;
            }

            return value;
        }

        public static int IntParse(this string number)
        {
            return IntParse(number, false);
        }

        public static int? NullableIntParse(this string number)
        {
            if (string.IsNullOrWhiteSpace(number))
            {
                return null;
            }

            return IntParse(number, false);
        }
    }
}